package com.eques.demo.adapter

/**
 * @description
 * @author Create by yc on 2025/10/29/Wed 11:31
 */

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.eques.bean.AlarmRecord
import com.eques.demo.R

class AlarmRecordAdapter : RecyclerView.Adapter<AlarmRecordAdapter.ViewHolder>() {

    private var alarmRecords: List<AlarmRecord> = emptyList()

    // 点击监听器
    var onItemClickListener: ((AlarmRecord) -> Unit)? = null

    fun setData(records: List<AlarmRecord>) {
        this.alarmRecords = records
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_alarm_record, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val record = alarmRecords[position]
        holder.bind(record)

        holder.itemView.setOnClickListener {
            onItemClickListener?.invoke(record)
        }
    }

    override fun getItemCount(): Int = alarmRecords.size

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val ivThumbnail: ImageView = itemView.findViewById(R.id.iv_thumbnail)
        private val tvCategoryTag: TextView = itemView.findViewById(R.id.tv_category_tag)
        private val tvAlarmType: TextView = itemView.findViewById(R.id.tv_alarm_type)
        private val tvCreateTime: TextView = itemView.findViewById(R.id.tv_create_time)
        private val tvDeviceId: TextView = itemView.findViewById(R.id.tv_device_id)
        private val tvAlarmDetail: TextView = itemView.findViewById(R.id.tv_alarm_detail)

        fun bind(record: AlarmRecord) {
            // 加载缩略图
            if (record.hasImage()) {
                Glide.with(itemView.context)
                    .load(record.getThumbnailUrl())
                    .placeholder(R.drawable.ic_person)
                    .error(R.drawable.ic_person)
                    .into(ivThumbnail)
            } else {
                ivThumbnail.setImageResource(R.drawable.ic_person)
            }

            // 设置报警类型
            tvAlarmType.text = record.getAlarmType()

            // 设置创建时间
            tvCreateTime.text = record.getFormattedTime()

            // 设置设备ID
            tvDeviceId.text = "设备ID: ${record.lock_id.take(8)}..."

            // 设置报警详情
            tvAlarmDetail.text = "文件类型: ${record.getCategoryType()} | 锁AID: ${record.lock_aid.take(8)}..."

            // 设置文件类型标签
            tvCategoryTag.text = record.getCategoryType()

            // 根据报警类型设置不同的颜色
            setAlarmTypeColor(record.alarm)
        }

        private fun setAlarmTypeColor(alarmType: Int) {
            val color = when (alarmType) {
                5 -> "#FF9800" // 门铃呼叫 - 橙色
                6 -> "#F44336" // 撬锁报警 - 红色
                7 -> "#FFEB3B" // 低电量 - 黄色
                else -> "#2196F3" // 其他 - 蓝色
            }
            tvAlarmType.setTextColor(android.graphics.Color.parseColor(color))
        }
    }
}